package com.testitems.enums;

import lombok.Getter;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author GFH
 * @since 2021-11-20
 */
public enum RoleEnum {
    SUPER_ADMIN(1),
    ADMIN(2),
    EMPLOYEE(3),
    ;

    @Getter
    private Integer id;

    RoleEnum(Integer id) {
        this.id = id;
    }


    private static Map<Integer, RoleEnum> cache = new HashMap<>();

    static {
        Arrays.stream(RoleEnum.values()).forEach(item -> cache.put(item.getId(), item));
    }

    public static RoleEnum valueOf(Integer type) {
        RoleEnum rule = cache.get(type);
        if (rule == null) {
            throw new IllegalArgumentException("不支持的类型：value = " + type);
        }
        return rule;
    }

    public static Map<String, Integer> getRoleMap() {
        final Map<String, Integer> responseMap = new HashMap<>();
        for (RoleEnum e : RoleEnum.values()) {
            responseMap.put(e.name(), e.getId());
        }
        return responseMap;
    }
}
